//
//  ImagePoster.m
//  TarPhone
//
//  Created by Christopher Anderson on 19/03/2009.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "TarPipeOAuthRequestPoster.h"


@implementation TarPipeOAuthRequestPoster

#define WORKFLOW_URL  @"http://api.tarpipe.net/2.0/workflow/start/%@"

@synthesize oauthAPI;

- (id) init {
	self = [super init];
	if (self) {
		TarPhoneAppDelegate *appDelegate =  (TarPhoneAppDelegate*)[[UIApplication sharedApplication] delegate];
		self.oauthAPI =  appDelegate.oauthAPI;
	}
	return self;
}



- (NSString*) postRequestToTarPipe:(TarpipeRequest*)inRequest {
	
	MPOAuthMultiPartImageRequestParameter *imageParam = [[MPOAuthMultiPartImageRequestParameter alloc] initWithName:@"image" image:inRequest.image mimeType:@"image/png"];
	MPOAuthMultiPartTextRequestParameter *titleParam = [[MPOAuthMultiPartTextRequestParameter alloc] initWithName:@"title" andValue:inRequest.title];
	MPOAuthMultiPartTextRequestParameter *bodyParam = [[MPOAuthMultiPartTextRequestParameter alloc] initWithName:@"body" andValue:inRequest.body];
	
	NSString *url = [NSString stringWithFormat:WORKFLOW_URL, inRequest.workflow._id];
	MPOAuthMultipartRequest *request = [[MPOAuthMultipartRequest alloc] initWithURL:[NSURL URLWithString:url] 
																	  andParameters:nil];
	[request addFileParameter:titleParam];
	[request addFileParameter:bodyParam];
	[request addFileParameter:imageParam];
	
	//TarPipeOAuthDelegatedRequest *requestDelegate = [[TarPipeOAuthDelegatedRequest alloc] initWithDelegate:delegate];
	NSData *result = [oauthAPI dataForRequest:request];
	return [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
	//[oauthAPI performRequest:request withTarget:requestDelegate andAction:@selector(methodLoadedFromURL:withResponseString:)];
	//[requestDelegate release];
	 
}


@end
